package com.example.my.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.my.entity.Role;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


@Mapper
public interface RoleMapper extends BaseMapper<Role> {


    @Select("SELECT r.* " +
            "FROM roles r " +
            "INNER JOIN user_role ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    List<Role> getRolesByUserId(@Param("userId") Long userId);

    @Select("SELECT r.id, r.name, r.description, r.role_key, " +
            "p.id AS perm_id, p.name AS perm_name, p.perm_code " +
            "FROM roles r " +
            "LEFT JOIN role_permission rp ON r.id = rp.role_id " +
            "LEFT JOIN permissions p ON rp.perm_id = p.id " +
            "WHERE r.id = #{roleId} AND r.deleted = 0")


    Role getRoleWithPermissions(@Param("roleId") Long roleId);
}